package com.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fragmenttest.Bean.User;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText usnedit, psdedit;
    private Button registerbtn, loginbtn;
    //    private RadioButton adminrdb,userrdb ;
    private RadioGroup radiogroup ;
    private Switch autoswitch ;
    private TextView autotxt ;

    //只要实例化这个控件RadioGroup，里面的就不用实例化了，他会一并实例化
    int CHOOSE_LEVEL=0; //定义一个

    List<User> userList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        /*
         *添加管理员
         */
        User user =new User(0,"admin","admin",2);
        User user1 =new User(0,"gly","123",2);

        user.save();
        user1.save();
        getUser();
        initView();
        setListener();
    }
    private void getUser() {
        List<User> userList = LitePal.findAll(User.class);
        if (userList.size() == 0) {
            User user = new User(0, "李光通", "123");
            User user1 = new User(1, "李琛", "456");
            User user2 = new User(2, "李志勇", "789");
            user.save();
            user1.save();
            user2.save();
        }
    }
    private void setListener() {


        /*自动填充按钮*/
        autoswitch .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()){//如果控件被选中，则提取粗、储存的账号密码，进行自动登录
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    usnedit.setText(sp.getString("username",""));
                    psdedit.setText(sp.getString("password",""));
                }else {
                    usnedit.setText("");
                    psdedit.setText("");
                }
            }
        });
        /*管理员和用户按钮控件*/
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){  //判断checkedid
                    case R.id.adminrdb:
                        CHOOSE_LEVEL=2;
                        break;
                    case R.id.userrdb:
                        CHOOSE_LEVEL=1;
                        break;
                }
            }
        });
        /*注册按钮*/
        registerbtn.setOnClickListener(new View.OnClickListener() {
            //注册
            @Override
            public void onClick(View v) {
                String usn = usnedit.getText().toString().trim();
                String psd = psdedit.getText().toString().trim();
                User user = new User(0, usn, psd); //
                if (user.save()) {
                    showToast("注册成功");
                } else {
                    showToast("用户已存在，请登录");

                    //save方法会返回一个布尔值，如果添加失败则说明重名
                }
            }
        });
        /* 登录按钮*/
        loginbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String usn = usnedit.getText().toString().trim();
                String psd = psdedit.getText().toString().trim();
                User user = confirmUser(usn, psd);
                if (user ==null) {
                    showToast("用户名或密码错误");
                } else  if (user.getPermission()==CHOOSE_LEVEL){//当你
                    SharedPreferences sharedPreferences = getSharedPreferences("userinfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("userid", user.getId());
                    editor.putString("username", user.getName());
                    editor.putString("password", user.getPassword());
                    editor.putInt("permission",user.getPermission());//
                    editor.apply();
                    showToast("登陆成功");
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }else {
                    showToast("权限选择错误");
                }
            }
        });
    }

    private void showToast(String str) {
        Toast.makeText(LoginActivity.this, str, Toast.LENGTH_SHORT).show();
    }
    private User confirmUser(String usn, String psd) {
        userList = LitePal.findAll(User.class);
        for (User u : userList) {//对userlist
            if (usn.equals(u.getName()) && psd.equals(u.getPassword())) {
                return u;//打断循环，并且返回值
            }
        }
        return  null ;
    }
    private void initView() {
        autoswitch=findViewById(R.id.autoswitch);
        autotxt=findViewById(R.id.autotxt);
        radiogroup=findViewById(R.id.radiogroup);
        // adminrdb=findViewById(R.id.adminrdb);
        //  userrdb=findViewById(R.id.userrdb);
        usnedit=findViewById(R.id.usnedit) ;
        psdedit=findViewById(R.id.psdedit) ;
        registerbtn=findViewById(R.id.registerbtn) ;
        loginbtn=findViewById(R.id.loginbtn) ;
    }
}
